package espace_physique;

import fichiers.Dossier;
import fichiers.Element;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import support.*;


public class SousVueEspaceDossierRadio extends JPanel implements VueInterface {
    
    private ButtonGroup groupe;
    private JRadioButton never,always;
    private JPanel droitRadio;
    private ImageIcon alwaysIcon,neverIcon,demarrageIcon;
    private ModelInterface model;
    private Dimension dim = new Dimension(260,90);
    
    public SousVueEspaceDossierRadio() {
        
        this.setLayout(null);
        
        this.alwaysIcon=new ImageIcon("spyIcon.jpg");
        this.neverIcon=new ImageIcon("neverIcon.jpg");

        
        this.setBorder(BorderFactory.createTitledBorder("Pour le dossier en cours"));
        
        this.groupe=new ButtonGroup();
        this.never=new JRadioButton("Supprimer de picasial");
        never.setEnabled(false)  ;
        this.always=new JRadioButton("Analyser en permanence");
        always.setEnabled(false);
        
        ControleurBoutonRadio ecouteurRadio=new ControleurBoutonRadio(this.always,this.never,this);
        this.always.addActionListener(ecouteurRadio);
        this.never.addActionListener(ecouteurRadio);
        
        
        groupe.add(this.always);
        groupe.add(this.never);
        
        JLabel alwaysLabel = new JLabel( this.alwaysIcon);
        JLabel neverLabel = new JLabel( this.neverIcon);
        
        
        JPanel haut = new JPanel(new FlowLayout());
        haut.add(neverLabel);
        haut.add(never);
        
        JPanel bas = new JPanel(new FlowLayout());
        bas.add(alwaysLabel);
        bas.add(always);
        
        haut.setBounds(4,20,200,30);
        bas.setBounds(12,50,200,30);
        this.add(haut);
        this.add(bas);
        

    }
    
    public void setModel(ModelInterface model){
        
        this.model=model;
        this.model.ajouterVue(this);
        this.mettreAJourVerification((Element) model);
    }
    
    public ModelInterface getModel(){
        
        return this.model;
    }
    
    public void mettreAJourVerification(Element courant) {
        
        this.setBorder(BorderFactory.createTitledBorder(courant.toString()));
        File ft=courant.getFichierCourant();
        if(ft==null){
            this.always.setEnabled(false);
            this.never.setEnabled(false);
            this.always.setSelected(false);
            this.never.setSelected(false);
        } else{
            
            this.always.setEnabled(true);
            this.never.setEnabled(true);
            
            if(((Dossier)courant).getVerification()==true){
                this.always.setSelected(true);
            }else{
                this.never.setSelected(true);
            }
            
        }
        
        
    }
    
    public void mettreAJourSuppression(Element courant) {
    }
    
    public void mettreAJourAjout(Element nouveau) {
    }
 
    public Dimension getSize() {
        return dim;
    }
    public Dimension getPreferredSize() {
        return dim;
    }
    public Dimension getMaximumSize() {
        return dim;
    }
    public Dimension getMinimumSize() {
        return dim;
    }
}


class ControleurBoutonRadio implements ActionListener{
    
    private JRadioButton always;
    private JRadioButton never;
    private SousVueEspaceDossierRadio vue;
    
    public ControleurBoutonRadio(JRadioButton always,JRadioButton never,SousVueEspaceDossierRadio vue) {
        
        this.never=never;
        this.always=always;
        this.vue=vue;
    }
    
    
    public void actionPerformed(ActionEvent e) {
        
        Object source=e.getSource();
        boolean resultat;
        if(source==never){
            resultat=false;
        }else{
            resultat=true;
        }
        
        ((Dossier)vue.getModel()).setVerification(resultat);
        
    }
    
    
    
}

